package com.tsinjie.metadata.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Types;

/**
 * Created by LiuXunming on 2015-10-14.
 * 转换工具类
 */
public class ConvertUtils {

    private static Logger logger = LoggerFactory.getLogger(ConvertUtils.class);
    /**
     * 表命名分隔符
     */
    private final static String SPLIT_STR = "_";

    /**
     * SQL type到jdbcType的转换(mybatis 配置文件)
     *
     * @param sqlType
     * @return
     */
    public static String convertSQLTypeToJdbcType(int sqlType) {
        String type = "VARCHAR";
        switch (sqlType) {
            case Types.BIT:
                type = "BIT";
                break;
            case Types.TINYINT:
                type = "TINYINT";
                break;
            case Types.SMALLINT:
                type = "SMALLINT";
                break;
            case Types.INTEGER:
                type = "INTEGER";
                break;
            case Types.BIGINT:
                type = "BIGINT";
                break;
            case Types.FLOAT:
                type = "FLOAT";
                break;
            case Types.REAL:
                type = "REAL";
                break;
            case Types.DOUBLE:
                type = "DOUBLE";
                break;
            case Types.NUMERIC:
                type = "NUMERIC";
                break;
            case Types.DECIMAL:
                type = "DECIMAL";
                break;
            case Types.CHAR:
                type = "CHAR";
                break;
            case Types.VARCHAR:
                type = "VARCHAR";
                break;
            case Types.LONGVARCHAR:
                type = "LONGVARCHAR";
                break;
            case Types.DATE:
                type = "DATE";
                break;
            case Types.TIME:
                type = "TIME";
                break;
            case Types.TIMESTAMP:
                type = "TIMESTAMP";
                break;
            case Types.BINARY:
                type = "BINARY";
                break;
            case Types.VARBINARY:
                type = "VARBINARY";
                break;
            case Types.LONGVARBINARY:
                type = "LONGVARBINARY";
                break;
            case Types.NULL:
                type = "NULL";
                break;
            case Types.OTHER:
                type = "OTHER";
                break;
            case Types.JAVA_OBJECT:

                break;
            case Types.DISTINCT:

                break;
            case Types.STRUCT:

                break;
            case Types.ARRAY:

                break;
            case Types.BLOB:
                type = "BLOB";
                break;
            case Types.CLOB:
                type = "CLOB";
                break;
            case Types.REF:

                break;
            case Types.DATALINK:

                break;
            case Types.BOOLEAN:
                type = "BOOLEAN";
                break;
            case Types.ROWID:

                break;
            default:
                break;
        }
        return type;
    }

    /**
     * SQL type到JAVA type的转换
     *
     * @param sqlType
     * @return
     */
    public static String convertSQLTypeToJavaType(int sqlType) {
        String type = "String";
        switch (sqlType) {
            case Types.BIT:
                type = "Boolean";
                break;
            case Types.TINYINT:
                type = "Byte";
                break;
            case Types.SMALLINT:
                type = "Short";
                break;
            case Types.INTEGER:
                type = "Integer";
                break;
            case Types.BIGINT:
                type = "Long";
                break;
            case Types.FLOAT:
                type = "Double";
                break;
            case Types.REAL:
                type = "Float";
                break;
            case Types.DOUBLE:
                type = "Double";
                break;
            case Types.NUMERIC:
                type = "BigDecimal";
                break;
            case Types.DECIMAL:
                type = "BigDecimal";
                break;
            case Types.CHAR:
                type = "String";
                break;
            case Types.VARCHAR:
                type = "String";
                break;
            case Types.LONGVARCHAR:
                type = "String";
                break;
            case Types.DATE:
                type = "Date";
                break;
            case Types.TIME:
                type = "Date";
                break;
            case Types.TIMESTAMP:
                type = "Date";
                break;
            case Types.BINARY:
                type = "Byte[]";
                break;
            case Types.VARBINARY:
                type = "Byte[]";
                break;
            case Types.LONGVARBINARY:
                type = "Byte[]";
                break;
            case Types.NULL:

                break;
            case Types.OTHER:

                break;
            case Types.JAVA_OBJECT:

                break;
            case Types.DISTINCT:

                break;
            case Types.STRUCT:

                break;
            case Types.ARRAY:

                break;
            case Types.BLOB:
                type = "Byte[]";
                break;
            case Types.CLOB:
                type = "String";
                break;
            case Types.REF:

                break;
            case Types.DATALINK:

                break;
            case Types.BOOLEAN:

                break;
            case Types.ROWID:

                break;
            default:
                break;
        }
        return type;
    }

    /**
     * 将表名转换成类名（去除t_${module}），驼峰格式
     *
     * @param tableName
     * @param hasModule
     * @return
     */
    public static String convertTableNameToClazzName(String tableName, boolean hasModule) {
        String[] array = tableName.split(SPLIT_STR);
        if (array.length == 1) {
            return upperCaseFirstChar(array[0], true);
        }
        int i = hasModule ? 2 : 1;
        StringBuilder className = new StringBuilder();
        while (i < array.length) {
            className.append(upperCaseFirstChar(array[i++], true));
        }
        return className.toString();
    }


    /**
     * 获取（module）模块名
     *
     * @param tableName
     * @param hasModule
     * @return
     */
    public static String getModuleNameByTableName(String tableName, boolean hasModule) {
        String[] array = tableName.split(SPLIT_STR);
        if (array.length == 1) {
            return null;
        }
        if (hasModule) {
            return array[1].toLowerCase();
        }
        return null;
    }

    /**
     * 首字母大写，其余字母小写
     *
     * @param str
     * @param lowerOthers 其余字母是否小写
     * @return
     */
    public static String upperCaseFirstChar(String str, boolean lowerOthers) {
        String firstChar = str.substring(0, 1).toUpperCase();
        if (str.length() == 1) {
            return firstChar;
        }
        String other = str.substring(1, str.length());
        if (lowerOthers) {
            other = other.toLowerCase();
        }
        return firstChar + other;
    }

    /**
     * 首字母小写
     *
     * @param str
     * @param lowerOthers 其余字母是否小写
     * @return
     */
    public static String lowerCaseFirstChar(String str, boolean lowerOthers) {
        String firstChar = str.substring(0, 1).toLowerCase();
        if (str.length() == 1) {
            return firstChar;
        }
        String other = str.substring(1, str.length());
        if (lowerOthers) {
            other = other.toLowerCase();
        }
        return firstChar + other;
    }

    /**
     * 将表字段名称，转换成java字段名，驼峰格式
     *
     * @param columnName
     * @return
     */
    public static String convertColumnNameToFieldName(String columnName) {
        String[] array = columnName.split(SPLIT_STR);
        StringBuilder varName = new StringBuilder(lowerCaseFirstChar(array[0], true));
        if (array.length == 1) {
            return varName.toString();
        }
        for (int i = 1; i < array.length; i++) {
            varName.append(upperCaseFirstChar(array[i], true));
        }
        return varName.toString();
    }

    /**
     * 转换成复数形式
     *
     * @param name
     * @return
     */
    public static String convertToPlural(String name) {
        return Inflector.getInstance().pluralize(name);
    }
}
